<?php
namespace app\index\controller;
use think\Cache;
use think\Db;
use think\Request;
use think\Controller;
use polyv\Blw as blws;

class Blw extends Base
{
    // 分页查询频道直播观看详情数据
    public function statistics()
    {
        $list =blws::getStatistics("3856732", "2023-03-31", "1" ,"100");
        Cache::set('ce','ouyang');
        dump(Cache::get('ce'));
        ;
        apiReturn(200,'获取公司总业绩成功',$list);
    }

    // 采集直播观看数据
    public function collectStatistics()
    {
        try {
            $blw = model('Blw');

            $channelId = "3856732";

            $flag = 0;
            $today_currentDay = date("Y-m-d",time());
            while ($flag < 1) {
                $chat_collect = db('chat_collect')->where(array('channelId'=>$channelId))->order('date DESC')->find();

                if (empty($chat_collect)) {
                    $currentDay = date('Y-m-d',time());
                    $chat_collect_datas = array(
                        'channelId'=>$channelId,
                        'currentDay'=>$currentDay,
                        'status'=>0,
                        'nextPageNumber'=>1,
                        'date'=>time()
                    );
                    db('chat_collect')->insert($chat_collect_datas);//第一条数据
                }else{
                    if ($chat_collect['status'] == 0) {
                        $currentDay = $chat_collect['currentDay'];
                    }else{
                        $currentDay = date('Y-m-d',strtotime($chat_collect['currentDay']." 00:00:00")+24*3600);
                        $chat_collect_datas = array(
                            'channelId'=>$channelId,
                            'currentDay'=>$currentDay,
                            'status'=>0,
                            'nextPageNumber'=>1,
                            'date'=>time()
                        );
                        db('chat_collect')->insert($chat_collect_datas);//下一天数据
                    }
                }
                $chat_collect = db('chat_collect')->where(array('channelId'=>$channelId))->order('date DESC')->find();

                $response = $blw->getStatistics($channelId, $currentDay, $chat_collect['nextPageNumber'], "1000");
                foreach ($response['data']['contents'] as $key => $value) {
                    $chat = db('chat')->where('playId',$value['playId'])->find();
                    if (empty($chat)) {
                        $chat_datas = array(
                            'playId'=>$value['playId'],
                            'userId'=>$value['userId'],
                            'channelId'=>$value['channelId'],
                            'playDuration'=>$value['playDuration'],
                            'stayDuration'=>$value['stayDuration'],
                            'sessionId'=>$value['sessionId'],
                            'param1'=>$value['param1'],
                            'param2'=>$value['param2'],
                            'param3'=>$value['param3'],
                            'currentDay'=>$value['currentDay'],
                            'createdTime'=>$value['createdTime'],
                            'lastModified'=>$value['lastModified'],
                            'ptype'=>$value['ptype'],
                            'firstActiveTime'=>$value['firstActiveTime'],
                            'lastActiveTime'=>$value['lastActiveTime'],
                            'date'=>time()
                        );
                        db('chat')->insert($chat_datas);//新增聊天
                    }
                }
                //最后一页
                if ($response['data']['lastPage']) {
                    $chat_collect_datas = array(
                        'nextPageNumber'=>$response['data']['nextPageNumber'],
                        'totalItems'=>$response['data']['totalItems'],
                        'date'=>time()
                    );
                    //不是今天
                    if ($today_currentDay != $currentDay) {
                        $chat_collect_datas['status'] = 1;
                    }
                    db('chat_collect')->where('id',$chat_collect['id'])->update($chat_collect_datas);
                    $flag = 1;
                }else{
                    $chat_collect_datas = array(
                        'nextPageNumber'=>$response['data']['nextPageNumber'],
                        'totalItems'=>$response['data']['totalItems'],
                        'date'=>time()
                    );
                    db('chat_collect')->where('id',$chat_collect['id'])->update($chat_collect_datas);
                }
            }
            exit;
        } catch (Exception $e) {
            return $e->getMessage();
        }

    }


}
